<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Backend_Form_User_Update
 *
 * @author Eugene
 */
class Backend_Form_User_Update extends Zend_Form
{

	/**
	 * @var int
	 */
	protected $_userId;

	/**
	 *
	 * @param int $userId
	 * @return Backend_Form_User_Update
	 */
	public function setUserId($userId)
	{
		$this->_userId = (int) $userId;

		return $this;
	}

	/**
	 *
	 * @return int
	 */
	public function getUserId()
	{
		return $this->_userId;
	}

	/**
	 *
	 * @return void
	 */
	public function init()
	{
		$this->setAction($this->getView()->url());
		$this->setMethod(Zend_Form::METHOD_POST);

		$this->addElement('text', 'name', array(
			'label' => 'Name',
			'required' => true,
			'filters' => array('StringTrim'),
			'validators' => array(
				array('Alnum'),
				array('StringLength', false, array(4, 255)),
			),
		));

		$this->addElement('select', 'role', array(
			'label' => 'Role',
			'required' => true,
			'multiOptions' => array(
				Application_Model_User::ROLE_MEMBER => 'Member',
				Application_Model_User::ROLE_MANAGER => 'Manager'
			)
		));

		$this->addElement('text', 'email', array(
			'label' => 'Email',
			'required' => true,
			'filters' => array('StringTrim'),
			'validators' => array(
				array('EmailAddress'),
				array('StringLength', false, array(6, 255)),
				array('Db_NoRecordExists', false, array(
						'table' => 'user',
						'field' => 'email',
						'exclude' => array(
							'field' => 'id',
							'value' => $this->getUserId()
						)
				))
			),
		));

		$this->addElement('password', 'newPassword', array(
			'label' => 'New password',
			'validators' => array(
				array('StringLength', false, array(6, 255))
			),
		));

		$this->addElement('password', 'newConfirm', array(
			'label' => 'New confirm',
			'validators' => array(
				array('StringLength', false, array(6, 255)),
				array('Identical', false, array('token' => 'newPassword'))
			),
		));

		$this->addElement('checkbox', 'enabled', array(
			'label' => 'Enabled'
		));

		$this->addElement('hash', 'csrf', array(
			'ignore' => true
		));

		$this->addElement('submit', 'update', array(
			'label' => 'Update',
			'ignore' => true
		));
	}

}